﻿Imports Tools4Fools.Interop
Imports Tools4Fools.Interop.Extensibility
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Imports Tools4Fools.Interop.VBAExtensibility
Imports System.Drawing
Imports Microsoft.Office.Core


''NB all of this code is from Andrew E

Module mIndent


    Public sIndentation As String
    Private oVBProj As VBProject
    Private oVBComp As VBComponent
    Private oVBMod As CodeModule
    Private lNumLines As Long
    Private lCntr As Long
    Private sTabs As String
    Private sCodeString As String
    Private bTrustAccess As Boolean

    Sub IndentProjectCode()
        Dim sProjectName As String

        '  On Error Resume Next


        oVBProj = VBE.ActiveVBProject

        For Each oVBComp In oVBProj.VBComponents

            With oVBComp.CodeModule

                sTabs = ""

                lNumLines = .CountOfLines * 2

                For lCntr = 1 To lNumLines

                    .ReplaceLine(lCntr, LTrim(.Lines(lCntr, 1)))

                    sCodeString = .Lines(lCntr, 1)

                    Select Case sCodeString

                        Case "End Sub", "End Function", "End Type", "End Enum", "End Property"
                            sTabs = ""

                        Case "End If", "#End If", "End Select", "End With", "Else", "#Else", "Loop"
                            sTabs = Replace(sTabs, vbTab, "", 1, 1)

                    End Select

                    If Left(sCodeString, 4) = "Next" Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                    If InStr(sCodeString, "Loop Until") > 0 Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                    If InStr(sCodeString, "Loop While") > 0 Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                    If InStr(sCodeString, "Wend") > 0 Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                    .ReplaceLine(lCntr, sTabs & .Lines(lCntr, 1))

                    If Left(sCodeString, 4) = "Sub " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 12) = "Private Sub " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 11) = "Public Sub " Then sTabs = sTabs & vbTab

                    If Left(sCodeString, 9) = "Function " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 17) = "Private Function " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 16) = "Public Function " Then sTabs = sTabs & vbTab

                    If Left(sCodeString, 5) = "Type " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 13) = "Private Type " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 12) = "Public Type " Then sTabs = sTabs & vbTab

                    If Left(sCodeString, 5) = "Enum " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 13) = "Private Enum " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 12) = "Public Enum " Then sTabs = sTabs & vbTab

                    If Left(sCodeString, 9) = "Property " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 17) = "Private Property " Then sTabs = sTabs & vbTab
                    If Left(sCodeString, 16) = "Public Property " Then sTabs = sTabs & vbTab

                    sCodeString = Left(sCodeString, InStr(sCodeString, " ") - 1)

                    Select Case sCodeString

                        Case "For", "With", "Select", "Else", "#Else", "Do"
                            sTabs = sTabs & vbTab

                    End Select

                    If sCodeString = "If" And Right(.Lines(lCntr, 1), 4) = "Then" Then
                        sTabs = sTabs & vbTab
                    End If

                    If sCodeString = "#If" And Right(.Lines(lCntr, 1), 4) = "Then" Then
                        sTabs = sTabs & vbTab
                    End If

                    If sCodeString = "If" And Right(.Lines(lCntr, 1), 1) = "_" And Right(.Lines(lCntr + 1, 1), 4) = "Then" Then
                        sTabs = sTabs & vbTab
                    End If

                    If sCodeString = "#If" And Right(.Lines(lCntr, 1), 1) = "_" And Right(.Lines(lCntr + 1, 1), 4) = "Then" Then
                        sTabs = sTabs & vbTab
                    End If

                Next lCntr

            End With

        Next oVBComp

        sProjectName = oVBProj.FileName

        sProjectName = Replace(sProjectName, Left(sProjectName, InStrRev(sProjectName, "/")), "")

        If sProjectName <> "" Then

            sIndentation = "Your '" & sProjectName & "' code is indented."

        Else

            sIndentation = "Your code is indented."

        End If

        MsgBox("sIndentation")

        oVBProj = Nothing

        On Error GoTo 0
    End Sub

    Public Sub IndentModuleCode()
        On Error Resume Next





        oVBMod = VBE.ActiveCodePane.CodeModule

        With oVBMod

            sTabs = ""

            lNumLines = .CountOfLines * 2

            For lCntr = 1 To lNumLines

                .ReplaceLine(lCntr, LTrim(.Lines(lCntr, 1)))

                sCodeString = .Lines(lCntr, 1)

                Select Case sCodeString

                    Case "End Sub", "End Function", "End Type", "End Enum", "End Property"
                        sTabs = ""

                    Case "End If", "#End If", "End Select", "End With", "Else", "#Else", "Loop"
                        sTabs = Replace(sTabs, vbTab, "", 1, 1)

                End Select

                If Left(sCodeString, 4) = "Next" Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                If InStr(sCodeString, "Loop Until") > 0 Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                If InStr(sCodeString, "Loop While") > 0 Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                If InStr(sCodeString, "Wend") > 0 Then sTabs = Replace(sTabs, vbTab, "", 1, 1)

                .ReplaceLine(lCntr, sTabs & .Lines(lCntr, 1))

                If Left(sCodeString, 4) = "Sub " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 12) = "Private Sub " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 11) = "Public Sub " Then sTabs = sTabs & vbTab

                If Left(sCodeString, 9) = "Function " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 17) = "Private Function " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 16) = "Public Function " Then sTabs = sTabs & vbTab

                If Left(sCodeString, 5) = "Type " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 13) = "Private Type " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 12) = "Public Type " Then sTabs = sTabs & vbTab

                If Left(sCodeString, 5) = "Enum " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 13) = "Private Enum " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 12) = "Public Enum " Then sTabs = sTabs & vbTab

                If Left(sCodeString, 9) = "Property " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 17) = "Private Property " Then sTabs = sTabs & vbTab
                If Left(sCodeString, 16) = "Public Property " Then sTabs = sTabs & vbTab

                sCodeString = Left(sCodeString, InStr(sCodeString, " ") - 1)

                Select Case sCodeString

                    Case "For", "With", "Select", "Else", "#Else", "Do"
                        sTabs = sTabs & vbTab

                End Select

                If sCodeString = "If" And Right(.Lines(lCntr, 1), 4) = "Then" Then
                    sTabs = sTabs & vbTab
                End If

                If sCodeString = "#If" And Right(.Lines(lCntr, 1), 4) = "Then" Then
                    sTabs = sTabs & vbTab
                End If

                If sCodeString = "If" And Right(.Lines(lCntr, 1), 1) = "_" And Right(.Lines(lCntr + 1, 1), 4) = "Then" Then
                    sTabs = sTabs & vbTab
                End If

                If sCodeString = "#If" And Right(.Lines(lCntr, 1), 1) = "_" And Right(.Lines(lCntr + 1, 1), 4) = "Then" Then
                    sTabs = sTabs & vbTab
                End If

            Next lCntr

            sIndentation = "Your '" & .Name & "' code is indented."

        End With


        MsgBox(sIndentation)


        oVBMod = Nothing

        On Error GoTo 0
    End Sub



End Module
